﻿using System;
using System.Collections.ObjectModel;
using Ay.Framework.WPF.Controls;
using Ay.MvcFramework;
using MPSystem.Models;
using Ay.Framework.WPF;
using MPSystem.Views;
using Base.DAL;
using Base.BLL;
using System.Windows.Forms;
using System.Windows.Controls;

namespace MPSystem.Controllers
{
    public class MPLoginController : Controller
    {
        Common common = new Common();
        public class MPLoginModel : Model
        {
            private string _UserID="Guest";

            /// <summary>
            /// 账户
            /// </summary>
            public string UserID
            {
                get { return _UserID; }
                set { Set(ref _UserID, value); }
            }

            private string _pwd;

            /// <summary>
            /// 密码
            /// </summary>
            public string pwd
            {
                get { return _pwd; }
                set { Set(ref _pwd, value); }
            }

            public int Flag { get; set; }=1;
            public bool IsRemote { get; set; } = true;
        }
        public MPLoginModel MPModel { get; set; } = new MPLoginModel();
        public static MPLoginModel CurUser = null;

        public MPLoginController() : base()
        {
            ViewBag.Title = common.GetSystemName().Replace("GB©","").Replace("®", "\r\n");
            //检测数据库
            if (!DbPretreat.CanConnect())
            {
                MessageBox.Show("数据库服务不可用！请检查数据库运行情况");
                Environment.Exit(0);
            }
            bool dbExist = DbPretreat.DbExists();
            bool needPretreat = DbPretreat.NeedPretreat();
            if (!dbExist || needPretreat)
            { 
                if(!dbExist)
                    MessageBox.Show("数据库不存在，请创建或指定一个现有数据库！");
                else
                    MessageBox.Show("您使用的可能是旧版数据库，请先进行升级！");
                try
                {
                    string pretreat = Application.StartupPath + @"\预处理\预处理.exe";
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo.FileName = pretreat;   //exe程序文件地址
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardInput = false;
                    p.StartInfo.RedirectStandardOutput = false;
                    p.StartInfo.RedirectStandardError = false;
                    p.StartInfo.CreateNoWindow = true;
                    //p.StartInfo.Arguments = sb.ToString();  //参数 多个参数使用空格分开               
                    p.Start();
                    p.WaitForExit();
                    p.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("请运行数据库预处理程序！");
                }
                finally
                {
                    Environment.Exit(0);
                }
            }
            
            Login = inParam =>
            {
                if (MPModel.UserID == null /*|| MPModel.pwd == null*/)
                {
                    AyMessageBox.Show("用户名不能为空！");
                    return;
                }
                if(MPModel.pwd == null)
                {
                    MPModel.pwd = "";
                }

                SYSTEMYHB yh = Db.Find<SYSTEMYHB>("SELECT * FROM SYSTEMYHB WHERE YHMC=@uid AND YHPAS=@psw", new { uid = MPModel.UserID, psw = MPModel.pwd });
                if (yh == null)
                {
                    AyMessageBox.Show("用户名/密码错误");
                }
                else
                {
                    //SYSTEMYHB yh = new SYSTEMYHB(); yh.FLAG = "0";
                    MPModel.Flag = int.Parse(yh.FLAG);
                    MPModel.IsRemote = Db.StrConn.IndexOf("Data Source=.") == -1;
                    if(!MPModel.IsRemote)
                        ComService.cs.Start();//开启串口
                    CurUser = MPModel;
                    AYUI.Message.Send<object>("登录成功！", MESSAGES.LoginSuccess);
                }
            };

        }

        /// <summary>
        /// 登录
        /// </summary>
        public ActionResult Login { get; private set; }

    }
}
